﻿using Yxinmt.Interface;
using Yxinmt.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yxinmt.Controls
{
    public class WindowsFormsHost : BaseControl, IControl
    {
        public System.Windows.Forms.Integration.WindowsFormsHost WpfWindowsFormsHost { get; set; }

        public WindowsFormsHost(Dictionary<string, object> property, IComponent component)
        {
            Property = property;
            _Component = component;

            WpfWindowsFormsHost = new System.Windows.Forms.Integration.WindowsFormsHost();
            this.Element = WpfWindowsFormsHost;

            InitSet();
            this.SetProperty();
            this.SetControlProperty(false, false);
        }

        public void SetChid(System.Windows.Forms.Control control)
        {
            WpfWindowsFormsHost.Child = control;
        }

        public bool HasChild()
        {
            return WpfWindowsFormsHost.Child != null;
        }

        public void SetWpfChild(System.Windows.FrameworkElement control)
        {
            System.Windows.Forms.Integration.ElementHost elementHost = new System.Windows.Forms.Integration.ElementHost();
            elementHost.Child = control;
            SetChid(elementHost);
        }

        private void SetProperty()
        {
        }

        public  IWindow Window
        {
            get { return _Window; }
        }

        protected override void SetProperty(string name, object value)
        {
            System.Windows.Forms.Integration.WindowsFormsHost c = WpfWindowsFormsHost;
            switch (name)
            {
                case "Name": c.Name = string.Concat("wfh_", (string)value); break;
                case "Height": c.Height = Convert.ToDouble(value); break;
                case "Width": c.Width = Convert.ToDouble(value); break;
                case "Dock": System.Windows.Controls.DockPanel.SetDock(c, this.GetDock((string)value)); break;
                case "BackColor": c.Background = GetColor((string)value); break;
                case "ControlAlign": SetControlAlign((string)value); break;
                case "Visible": SetVisible((bool)value); break;
                case "GridRowColumn": SetGridRowColumn((string)value); break;
                case "BackImageUrl": SetBackgroundImage((string)value); break;
                case "ZIndex": SetZIndex((int)value); break;
            }
        }

        public override void EventLoad()
        {
            base.EventLoad();

            bool isClick = Property.GetValue<bool>("IsClick");
            bool isDoubleClick = Property.GetValue<bool>("IsDoubleClick");

            if (isClick || isDoubleClick)
            {
                BindClick(isClick, isDoubleClick);
            }
        }
    }
}
